package org.crazyit.album.web;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.*;

import org.crazyit.album.exception.AlbumException;
import org.crazyit.album.web.base.BaseServlet;

@WebServlet(urlPatterns = "/validateName")
public class ValidateNameServlet extends BaseServlet {
    public void service(HttpServletRequest request
            , HttpServletResponse response) throws IOException, ServletException {
        String name = request.getParameter("user");
        response.setContentType("text/javascript;charset=gbk");
        PrintWriter out = response.getWriter();
        try {
            if (name != null) {
                if (as.validateName(name)) {
                    out.println("alert('');");
                } else {
                    out.println("alert('');");
                    out.println("$('#user').val('');");
                }
            } else {
                out.println("alert('');");
            }
        } catch (AlbumException ex) {
            out.println("alert('" + ex.getMessage() + "');");
        }
    }
}
